package leetcode.code1823;

import leetcode.IDebug;

public class Solution implements IDebug {

	class Node {
		int val;
		Node next;

		public Node(int val) {
			this.val = val;
		}
	}

	public int findTheWinner(int n, int k) {
		if (n == 1) {
			return 1;
		}
		if (k == 1) {
			return n;
		}
		Node L = new Node(1);
		Node R = L;
		for (int i = 2; i <= n; i++) {
			Node cur = new Node(i);
			R.next = cur;
			R = cur;
		}
		R.next = L;
		while (L.val != L.next.val) {
			int cnt = k - 2;
			while (cnt > 0) {
				L = L.next;
				cnt--;
			}
			L.next = L.next.next;
			L = L.next;
		}
		return L.val;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
